/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "SoundLoader.h"
#include <iostream>
#include <algorithm>
#include "../kernel.h"

extern Kernel* g_kernel;

#define PRINT( str ) (g_kernel->LPrintf( str ))


SoundLoader::SoundLoader(void)
{
    report_f.Set( this );
    g_kernel->GetConsole()->AddCommand( &report_f, "sound_report" );
}
  
/** Load a sound from a file */
SoundSource* SoundLoader::LoadSound( const std::string &s )
{
	if ( m_sounds.find( s ) == m_sounds.end() )
	{
		// create the new sound
		SoundSource* sound = new SoundSource;
		// load the sound
        if ( sound->Load( s ) ) {
            g_kernel->LPrintf( ("**Error Loading Sound: " + s).c_str() );
            
            //delete sound;
		    //return NULL;
        }
		m_sounds[s] = sound;
		return sound;
	}

	return m_sounds[s];
}

/** Test to see if the sound exists */
bool SoundLoader::HasSound(SoundSource* sound) const
{
    std::map<std::string, SoundSource*> ::const_iterator it = m_sounds.begin();
    for(; it != m_sounds.end(); )
    {
        if ( it->second == sound)
        {
            return true;
        }
        ++it;
    }
    return false;
}

/** Delete a sound */
void SoundLoader::Delete(SoundSource* sound)
{
    std::map<std::string, SoundSource*> ::iterator it = m_sounds.begin();
    for(; it != m_sounds.end(); )
    {
        if ( it->second == sound)
        {
            sound->Destroy();
            m_sounds.erase( it );
            // clear mem
            delete sound;
            sound = NULL;
            return;
        }
        ++it;
    }
}


/** Clear the loaded sounds */
void SoundLoader::Clear()
{   // delete the buffers
    std::map<std::string, SoundSource*> ::iterator it = m_sounds.begin();
    for(; it != m_sounds.end(); )
    {
        if ( it->second )
        {
            it->second->Destroy();
            delete it->second;
        }
        ++it;
    }
    m_sounds.clear();
}

SoundLoader::~SoundLoader(void)
{
    g_kernel->GetConsole()->RemoveCommand( "sound_report" );
    Clear();
}



/**
==========================
Sound Report, show all loaded sounds
==========================
*/
void SoundLoader::S_Report_f::Exec( std::string &s )
{
    if ( loader )
    {
        g_kernel->LPrintf( "------------Sound System Report------------------" );
        std::map<std::string, SoundSource*> ::iterator it = loader->m_sounds.begin();
        for(; it != loader->m_sounds.end(); )
        {
            g_kernel->LPrintf( it->first.c_str());
            g_kernel->LPrintf(  "--- Freq: %3.2f Bytes: %5d", it->second->GetFrequency(), it->second->GetSizeInBytes() );
            ++it;
        }
        g_kernel->LPrintf( "-------------------------------------------------" );
        g_kernel->LPrintf( "There are %d sounds in memory." , loader->NumSounds() );
    }
}